<script setup lang="ts">
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { PageWrapper } from '@/components/Page';
  import { useDrawer } from '@/components/Drawer';
  import { reactive, ref } from 'vue';
  import { message } from 'ant-design-vue';
  import { MessageSource } from '@/api/resource/resourceAllApi';
  import CloudTree from '@/views/resource/messageSource/CloudTree.vue';
  import SourceDrawer from '@/views/resource/messageSource/SourceDrawer.vue';
  import { columns, searchFormSchema } from '@/views/resource/messageSource/util/source.data';

  const emit = defineEmits(['selectRow']);
  const { pageList, del } = MessageSource();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const searchInfo = reactive<Recordable>({});
  const cloudOpt = ref(null);
  const isCreate = ref(true);
  const [registerTable, { reload }] = useTable({
    title: '消息源列表',
    api: pageList,
    rowKey: 'userId',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
    },
  });
  function handleCreate() {
    const record: Recordable = {
      cloudOption: cloudOpt.value,
    };
    openDrawer(true, {
      record,
      isUpdate: false,
    });
  }
  function handleEdit(record: Recordable) {
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }
  function handleSelect(cloudOption = '') {
    searchInfo.cloudOption = cloudOption;
    cloudOpt.value = cloudOption;
    isCreate.value = !cloudOption.includes(':');
    reload();
  }
  function handleSuccess() {
    message.success('新增成功');
    reload();
  }
  async function handleDelete(record: Recordable) {
    await del(record.sourceId);
    message.success('删除成功');
    await reload();
  }
  function rowClick(record) {
    console.log(record);
    emit('selectRow', record);
  }
</script>

<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <CloudTree class="w-1/4 xl:w-1/5" @select="handleSelect" />
    <BasicTable
      @register="registerTable"
      class="w-3/4 xl:w-4/5"
      :searchInfo="searchInfo"
      @row-click="rowClick"
    >
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" :disabled="isCreate">新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <SourceDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>
